import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../widgets/text_button.dart';
import '../../widgets/yz_text.dart';
import 'api_query_controller.dart';

class ApiQueryPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<ApiQueryController>(
        init: ApiQueryController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(
                title: Text('接口查询'),
                actions: [
                  Container(
                      margin: EdgeInsets.only(right: 15),
                      child: Text(
                        '数量:' + controller.codes.length.toString(),
                        style: TextStyle(color: Colors.white),
                      ))
                ],
              ),
              body: Container(
                  height: windowHeight,
                  margin: EdgeInsets.all(10),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          MyTextField(
                            controller: controller.textEditingController,
                            width: windowWidth * 0.5,
                            hintText: '请输入接口方法或描述',
                          ),
                          _textButton('搜索', onPressed: () {
                            controller.search();
                          }),
                        ],
                      ),
                      Expanded(
                          child: GridView.builder(
                              itemCount: controller.codes.length,
                              gridDelegate:
                                  SliverGridDelegateWithFixedCrossAxisCount(
                                      //横轴元素个数
                                      crossAxisCount: 4,
                                      //纵轴间距
                                      mainAxisSpacing: 15.0,
                                      childAspectRatio: 5,
                                      //横轴间距
                                      crossAxisSpacing: 15),
                              itemBuilder: (BuildContext context, int index) {
                                //Widget Function(BuildContext context, int index)
                                return _itemLayout(index, controller);
                              }))
                    ],
                  )));
        });
  }

  _itemLayout(int index, ApiQueryController controller) {
    final item = controller.codes[index];
    return InkWell(
        onDoubleTap: () {
          controller.copy(item.code!);
        },
        child: Container(
            color: Color(0xffE0EEF9),
            padding: EdgeInsets.only(left: 15, top: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.code!,
                  style: TextStyle(
                      color: Colors.blueAccent, fontWeight: FontWeight.bold),
                ),
                SizedBox(
                  height: 3,
                ),
                Text(item.introduce!)
              ],
            )));
  }

  _textButton(String text, {double width = 90, Function? onPressed}) {
    return Container(
        height: width,
        margin: EdgeInsets.only(left: 15, right: 20),
        alignment: Alignment.center,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            backgroundColor: Colors.deepPurpleAccent,
            textStyle: TextStyle(color: Colors.white)));
  }
}
